var app = getApp();
Page({

  onLoad: function (options) {
    //if (!app._checkUserInfo(app, 1)) { return } //检测用户是否登陆
  },


  _getFormData(e){
    var t = this
    var roomList = wx.getStorageSync("roomList")

    if (e.detail.value){
      var roomName = e.detail.value.roomName.trim();
      if (!/^[a-zA-Z]\w{1,10}$/.test(roomName)) {
        wx.showToast({
          title: '房间名称非法！',
          icon: 'loading'
        })
        return;
      }

      var roomPwd = e.detail.value.room_pwd;
      if (!roomPwd){
        wx.showToast({
          title: '密码不得为空！',
          icon:'loading'
        })
        return;
      }



      var userCount = e.detail.value.user_count;
      if (!/^[1-9][0-9]{0,1}$/.test(userCount)) {
        wx.showToast({
          title: '房间人数非法！',
          icon: 'loading'
        })
        return;
      }
      if (userCount < 2){
        wx.showToast({
          title: '不得少于2人！',
          icon: 'loading'
        })
        return;
      }
      var total = e.detail.value.total;
      if (!/^[1-9][0-9]{0,8}$/.test(total)) {
        wx.showToast({
          title: '房间分数非法！',
          icon: 'loading'
        })
        return;
      }

      if(total < userCount * 2){
        wx.showToast({
          title: '分数过少！',
          icon: 'loading'
        }) 
        return;
      }

      t.setData({
        roomName: roomName,
        roomPwd: roomPwd,
        userCount: userCount,
        total: total
      })
    }else{

      t.setData({
        header: e.detail.userInfo.avatarUrl,
        nickName: e.detail.userInfo.nickName,
      })
    }


    if (t.data.header && t.data.nickName && t.data.roomName && t.data.roomPwd && t.data.total && t.data.userCount && !t.data.createStat) {
      for(var i=0;i<roomList.length;i++){
        if (roomList[i].roomName == t.data.roomName){
          wx.showToast({
            title: '房间名已存在！',
            icon: 'loading'
          })
          return;
        }
      }

      t.setData({
        createStat:1    //打开创建开关
      })


      var data = {
        roomName: t.data.roomName,
        roomPwd: t.data.roomPwd,
        userCount: t.data.userCount,
        total: t.data.total,
        cheader: t.data.header,
        cname: t.data.nickName,
      }
      wx.showLoading({
        title: '创建中......'
      })
      app._callYunFun(data, 'fillPit_createRoom', function (res) {
        wx.hideLoading()
        if (res.result && res.result._id) {
          var tmpRoom = {
            _id: res.result._id,
            roomName: t.data.roomName,
            roomPwd: t.data.roomPwd
          }
          roomList.unshift(tmpRoom)
          wx.setStorageSync("roomList", roomList)
          wx.showToast({
            title: '房间创建成功！',
            duration: 1500
          })

          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/mt_fillPit_index/mt_fillPit_index',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: '房间名已存在！',
            icon: 'loading'
          })
        }
        //清空创建数据
        t.setData({
          roomName: '',
          roomPwd: '',
          userCount: '',
          total: 0,
          header: '',
          nickName: '',
          createStat:0
        })
      });
    }
    return

      var roomList = wx.getStorageSync("roomList")

    //app._fillPit_truncateRoom(app); return //清空表 


    //遍历插入多条数据
    // var nameStrs = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's']
    // wx.showLoading({
    //   title: '创建中......'
    // })
    // for (var i = 0; i < 200; i++) {
    //   var name = nameStrs[Math.floor(Math.random()*nameStrs.length)]+Math.ceil(Math.random()*100)
    //   var data = {
    //     roomName: name,
    //     roomPwd: roomPwd,
    //     userCount: userCount,
    //     total: total,
    //     cheader: app.globalData.header,
    //     cname: app.globalData.nickName,
    //   }
    //   app._callYunFun(data, 'fillPit_createRoom', function (res) {
    //     app.C(res.result);
    //   });
    // }
    // wx.hideLoading()



    var data = {
      roomName: roomName,
      roomPwd: roomPwd,
      userCount: userCount,
      total: total,
      cheader: app.globalData.header,
      cname:app.globalData.nickName,
    }
    wx.showLoading({
      title: '创建中......'
    })
    app._callYunFun(data,'fillPit_createRoom', function(res){
      wx.hideLoading()
      if(res.result && res.result._id){
        var tmpRoom = {
          _id: res.result._id,
          roomName: roomName,
          roomPwd:roomPwd
        }
        roomList.unshift(tmpRoom)
        wx.setStorageSync("roomList",roomList)
        wx.showToast({
          title: '房间创建成功！',
          duration: 1500
        })
        setTimeout(function(){
          wx.redirectTo({
            url: '/pages/mt_fillPit_index/mt_fillPit_index',
          })
        },1500)
      }else{
        wx.showToast({
          title: '房间名已存在！',
          type:'loading'
        })
      }
    });
  },


  data: {

  },



})